import * as ol from '../openLayer'
import {viewerDefaultConfig} from '../../config'
// import { deepAssign,deepCopyData } from '@/utils'
import { deepAssign ,deepCopyData} from '@/utils/tools/index.js'

/**
 * 新增 多个Overlay
 */
export function addMutilOlOverlay (map,options=[]){
  // console.log('options==',options)
  if(options.length>0){
    for(let item of options){

      addOlOverlay(map,item)
    }
  }
}
/**
 * 新增 单个Overlay
 */
export function addOlOverlay (map,options={}){
  options = deepAssign(viewerDefaultConfig, options) //  配置项
 
 let overlayPopup = new ol.Overlay({
    id: options?.id,
    element: options?.element,
    offset: options?.offset||[0, -20],
    positioning: options?.positioning ||'center-center' ,
    position:options?.position,
    autoPan: {
      animation: {
        duration: 200
      }
    }
 })

  // console.log('overlayPopup',overlayPopup)
  map.addOverlay(overlayPopup)
}
/**
 * 删除所有覆盖层
 */
export function removeAllRemoveOverlay (map) {
  let overlayArrs = map.getOverlays().array_
  // console.log('overlayArrs', overlayArrs)
  for (let overlay of overlayArrs) {
    // console.log('overlay', overlay)
    map.removeOverlay(overlay)
  }
}
/**
 * 注销控件
 */
export function disposeRemoveOverlay (map) {
  removeAllRemoveOverlay(map)
}
/**
 * 删除所有交互
 */
export function removeAllInteraction (map) {
  let interactionArrs = map.getInteractions().array_
  // console.log('interactionArrs', interactionArrs)
  for (let interaction of interactionArrs) {
    // console.log('interaction', interaction)
    map.removeInteraction(interaction)
  }
}
/**
 * 注销控件
 */
export function disposeInteraction (map) {
  removeAllInteraction(map)
}
/**
 * 删除所有控件
 */
export function removeAllControls (map) {
  let controlArrs = map.getControls().array_
  // console.log('controlArrs', controlArrs)
  for (let control of controlArrs) {
    // console.log('control', control)
    map.removeControl(control)
  }
}

/**
 * 注销控件
 */
export function disposeControl (map) {
  removeAllControls(map)
}
/**
 * 删除layer
 * @param {*} map
 * @param {*} key
 */
export function removeLayer (map, key, value) {
  // console.log('删除')
  let layerArr = map.getAllLayers()
  let currentLayerArr = layerArr.filter(item => {
    // console.log('item', item, item.get('id'), currentLayerKey, currentLayer)
    if (key === 'className') {
      return item.className_ === value
    } else {
      return item.get(key) === value
    }
  })
  // console.log('currentLayerArr', currentLayerArr)
  if (currentLayerArr.length > 0) {
    map.removeLayer(currentLayerArr[0])
  } else {
    // alert('未找到该图层')
  }
}
/**
 * 删除所有图层
 * @param {*} map
 */
export function removeAllLayer (map) {
  let layerArrs = map.getAllLayers()
  for (let layer of layerArrs) {
    map.removeLayer(layer)
  }
}
/**
 * 图层注销
 * @param {*} map
 */
export function disposeLayer (map) {
  removeAllLayer(map)
}
/**
 * 获取layer
 */
export function getMapLayer (map, key, value) {
  let layerArr = map.getAllLayers()
  let Layer = layerArr.filter(item => {
    if (key === 'className') {
      return item.className_ === value
    } else {
      return item.get(key) === value
    }
  })
  // console.log('Layer', Layer)
  return Layer
}

/**
 * 创建TileLayer
 * @param {*} map
 * @param {*} layers
 */
export function createTileLayer(map, layers) {
  for (let layer of layers.source ) {
    let TileLayer = new ol.TileLayer({
      title: layers.title,
      properties: layer.properties,
      source: new ol.XYZ({
        visible: true,
        wrapX: true,
        url: layer.url,
        attributions: '地图数据 &copy; Google Maps', // 显示的Attributions归属信息
      })
    })
    map.addLayer(TileLayer)
  }
}
/**
 * 获取TileLayers
 * @param {*} layers
 * @returns
 */
// export function getTileLayers (layers) {
//   let layerArr = []
//   for (let layer of layers) {
//     let TileLayer = new ol.TileLayer({
//       properties: layer.properties,
//       source: new ol.XYZ({
//         visible: true,
//         wrapX: true,
//         url: layer.url + '{z}/{x}/{y}.jpg'
//       })
//     })
//     layerArr.push(TileLayer)
//   }
//   return layerArr
// }
